package createThread;

/**
 * 终止线程：
 * 1.interrupt(),通过Thread.interrupted()检测
 *2.stop()可能导致原子性问题
 * 3.suspend()不会释放锁，其他线程无法获取该对象锁,造成死锁
 * @author: luolidong
 * @create: 2021-07-08 14:37
 * @version: 1.0
 */
public class StopThread {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Runnable runnable=()->{
            synchronized (o){
                while (!Thread.interrupted()) {
                    try {
                    System.out.println(Thread.currentThread().getName()+"111");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable,"A");
        thread.start();
        Thread thread1 = new Thread(runnable,"B");
        thread1.start();
        boolean interrupted = thread.isInterrupted();
        System.out.println(interrupted);
        System.out.println(thread.getState());
        Thread.sleep(1000);
//        thread.suspend(); // 不会释放锁，其他线程无法获取该对象锁
//        thread.stop();//stop方法可能导致原子性问题
        System.out.println(thread.getState());
        Thread.sleep(1000);
//        thread.resume();

        Thread.sleep(2000);
        //2秒后终止线程
//        thread.interrupt();
//        thread.stop();//stop方法可能导致原子性问题
    }

}
